<?php
namespace app\admin\controller\user;

use app\Request;
use app\admin\controller\AuthController;

use learn\services\UtilService as Util;

use FormBuilder\Factory\Elm;
use learn\services\FormBuilderService as Form;


use app\admin\model\user\User as UserModel;
use app\admin\model\user\UserBasic as BasicModel;
use app\admin\model\product\LoanRecord;


class User extends AuthController
{

    protected $noNeedLogin = ['index'];

    /**
     * 首页
     */
    public function index ()
    {
        return $this->fetch();
    }

    /**
     * 获取列表
     */
    public function lst ()
    {
        $where = Util::postMore([
            ['type',""],
            ['name',''],
            ['start_time',''],
            ['end_time',''],
            ['status',''],
            ['page',1],
            ['limit',20],
        ]);
        return app("json")->layui(UserModel::systemPage($where));
    }

    /**
     * 修改
     */
    public function edit ($uid="")
    {
        if (!$uid) return app("json")->fail("账号uid不能为空");
        $userinfo = UserModel::getUserInformation($uid);
        if (!$userinfo) return app("json")->fail("没有该用户");
        $form = array();
        $form[] = Elm::input('uid','登录账号',$userinfo['uid'])->disabled(1);
        $form[] = Elm::input('real_name','真实姓名',$userinfo['real_name']);
        $form[] = Elm::date('birthday','生日',$userinfo['birthday'] ? date('Y-m-d',$userinfo['birthday']) : 0);
        $form[] = Elm::input('card_id','身份证号',$userinfo['card_id']);
        $form[] = Elm::textarea('mark','用户备注',$userinfo['mark']);
        $form[] = Elm::radio('status','状态',$userinfo['status'])->options([['label'=>'启用','value'=>"1"],['label'=>'冻结','value'=>"0"]])->col(10);
        $form = Form::make_post_form($form, url('update',["uid"=>$uid])->build());
        $this->assign(compact('form'));
        return $this->fetch("public/form-builder");
    }

    /**
     * 保存修改
     */
    public function update ($uid = "")
    {
        //修改的用户数据
        $data = Util::postMore([
            ['real_name',''],
            ['birthday',''],
            ['card_id',''],
            ['mark',''],
            ['status','']
        ]);

        if(!$uid) return $this->failed('数据不存在');
        $userinfo = UserModel::getUserInformation($uid);
        if (!$userinfo) return app("json")->fail("数据不存在");
        $data['edit_time'] = time();
        $data['edit_id'] = $this->adminId;
        return UserModel::where("uid",$uid)->update($data) ? app("json")->success("操作成功",'code') : app("json")->fail("操作失败");
    }

    

    /**
     * 修改字段
     * @param $id
     * @return aModel
     */
    public function field ($uid)
    {
        if (!$uid) return app("json")->fail("参数有误，Id为空！");
        $where = Util::postMore([['field',''],['value','']]);
        if ($where['field'] == '' || $where['value'] =='') return app("json")->fail("参数有误！");
        return UserModel::update([$where['field'] => $where['value']],['uid'=>$uid]) ? app("json")->success("操作成功") : app("json")->fail("操作失败");
    }


    /**
     * 启用
     * @param Request $request
     * @return
     */
    public function userEnabled (Request $request)
    {
        $ids = $request->param("uid",0);
        if (empty($ids) || !$ids) return app("json")->fail("参数有误，UId为空！");
        if (!is_array($ids)) $ids = explode(",",$ids);
        return UserModel::where("uid","in",$ids)->update(['status'=>1]) ? app("json")->success("操作成功") : app("json")->fail("操作失败");
    }

    /**
     * 禁用
     * @param Request $request
     * @return
     */
    public function userDisabled (Request $request)
    {
        $ids = $request->param("uid",0);
        if (empty($ids) || !$ids) return app("json")->fail("参数有误，UId为空！");
        if (!is_array($ids)) $ids = explode(",",$ids);
        return UserModel::where("uid","in",$ids)->update(['status'=>0]) ? app("json")->success("操作成功") : app("json")->fail("操作失败");
    }

    /**
     * 认证信息
     */
    public function show($id)
    {
        $basicList = getConfig();
        $userInfo = UserModel::where("uid",$id)->find()->toArray();
        //月收入
        $userInfo['monthly_income_name'] = isset($basicList['income'][$userInfo['monthly_income']]) ? $basicList['income'][$userInfo['monthly_income']] : "暂无";
        //公积金 
        $userInfo['accumulation_name'] = $userInfo['accumulation'] == 0 ? '暂无' : $basicList['accumulation'][$userInfo['accumulation']];
        //职业类型
        $userInfo['occupation_name'] = isset($basicList['occupation'][$userInfo['occupation']]) ? $basicList['occupation'][$userInfo['occupation']] : '暂无';
        //行业
        $userInfo['industry_name'] = isset($basicList['industry'][$userInfo['industry']]) ? $basicList['industry'][$userInfo['industry']] : '暂无';
        //借款理由
        $userInfo['borrowing_name'] = isset($basicList['borrowing'][$userInfo['borrowing']]) ? $basicList['borrowing'][$userInfo['borrowing']] : '暂无';
        //负债情况
        $userInfo['liabilities_name'] = isset($basicList['liabilities'][$userInfo['liabilities']]) ? $basicList['liabilities'][$userInfo['liabilities']] : '暂无';

        $userBasic = BasicModel::where("uid",$id)->find();
        if($userBasic){
            $userBasic = $userBasic->toArray();
        } else{
            $userBasic['marriage'] = 0;
            $userBasic['address'] = "";
            $userBasic['education'] = 0;
            $userBasic['company_address'] = "";
            $userBasic['license_positive'] = "";
            $userBasic['license_side'] = "";
            $userBasic['room'] = "";
            $userBasic['insurance'] = "";
            $userBasic['detail_address'] = "";
            $userBasic['company_name'] = "暂无";
            $userBasic['company_detail_address'] = "暂无";
        }
        //婚姻状况 
        $userBasic['marriage_name'] = isset($basicList['marriage'][$userBasic['marriage']]) ? $basicList['marriage'][$userBasic['marriage']] : '暂无';
        //住宅地址
        $userBasic['address_name'] = isset($userBasic['address']) ? $userBasic['address'] : '';
        //学历
        $userBasic['education_name'] = isset($basicList['education'][$userBasic['education']]) ? $basicList['education'][$userBasic['education']] : '暂无';
        //公司地址
        $userBasic['company_address_name'] = isset($userBasic['company_address']) ? $userBasic['company_address'] : '';
        //行驶证
        $userBasic['license_name'] = ($userBasic['license_positive'] != "" && $userBasic['license_side'] != "") ? '存在信息' : "不存在";
        //房
        $userBasic['room'] = $userBasic['room'] != "" ?  '存在信息' : '不存在';
        //保单凭证
        $userBasic['insurance_name'] = $userBasic['insurance'] != "" ? '存在信息' : '不存在';
        $this->assign("userinfo",$userInfo);
        $this->assign("userbasic",$userBasic);
        return $this->fetch();
    }

    /**
     * 申请产品
     */
    public function productList($uid)
    {
        $this->assign("uid",$uid);
        return $this->fetch();
    }

    /**
     * 申请产品列表
     */
    public function productListLis($uid)
    {
        $where = Util::postMore([
            ['page',1],
            ['limit',20],
        ]);
        if($uid == 0) return app("json")->fail("无数据");
        return app("json")->layui(LoanRecord::systemPage($where,$uid));
        
    }


}
